package com.itwn.comic.web;

import com.github.pagehelper.PageInfo;
import com.itwn.comic.entity.Manager;
import com.itwn.comic.reult.ResponseResult;
import com.itwn.comic.service.ManagerService;
import com.itwn.comic.utils.JwtUtil;
import com.itwn.comic.vo.JwtInfo;
import com.itwn.comic.vo.ManagerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@RestController
@RequestMapping("manager")
public class ManagerController {

    @Autowired
    ManagerService managerService ;

    /**
     * -- 管理员的登录功能
     * @param manager
     * @return
     */
    @PostMapping("/Login")
    public ResponseResult<Manager> login(@RequestBody Manager manager){

        ResponseResult<Manager> result = new ResponseResult<>() ;
        try {
            Manager loginManager = managerService.queryManagerByName(manager.getName()) ;
            if (loginManager == null){
                result.setCode(201);
                result.setMsg("抱歉,没有此用户信息,请先注册");
            }else {
                if (loginManager.getPassword().equals(manager.getPassword())){
                    JwtInfo jwtInfo = new JwtInfo();
                    jwtInfo.setUserName(manager.getName());
                    String token = JwtUtil.creatToken(jwtInfo);
                    result.setCode(200);
                    result.setToken(token);
                    result.setMsg("恭喜你,登录成功");
                    result.setData(loginManager);
                }else {
                    result.setCode(201);
                    result.setMsg("抱歉,密码错误,请确认之后重试");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("抱歉,系统错误,请稍后重试");
        }

        return result ;
    }

    /*
     * @param now
     * @param size
     * @param name
     * @return
     *
     * 查询所有的管理员(分页)
     */
    @GetMapping("/managerList")
    public ResponseResult<PageInfo<ManagerVo>> managerList(@RequestParam(value = "now" , defaultValue = "1") Integer now ,
                                                         @RequestParam(value = "size" , defaultValue = "2") Integer size ,
                                                         String name){
        ResponseResult<PageInfo<ManagerVo>> result = new ResponseResult<>() ;

        try {
            PageInfo<ManagerVo> pageInfo = managerService.queryManagerByPage(now , size , name) ;
            result.setCode(200);
            result.setMsg("恭喜你,查询成功");
            result.setData(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("抱歉,查询失败");
        }

        return result ;
    }

    /*
     * @param manager
     * @return
     *
     * 管理员的添加功能
     */
    @PostMapping("/addManager")
    public ResponseResult<Void> addManager(@RequestBody ManagerVo managerVo){

        ResponseResult<Void> result = new ResponseResult<>() ;
        try {
            managerService.addManager(managerVo) ;
            result.setCode(200);
            result.setMsg("恭喜你,添加成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("抱歉,添加失败");
        }

        return result ;
    }

    /**
     * @param 
     * @return
     *
     * 管理员的修改功能
     */
    @PutMapping("/modifyManager")
    public ResponseResult<Void> modifyManager(@RequestBody ManagerVo managerVo){
        managerVo.setUpdateTime(new Date());
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            this.managerService.editManager(managerVo);
            result.setCode(200);
            result.setMsg("恭喜你,修改成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("抱歉,修改失败");
        }

        return result ;
    }

    /**
     * @param id
     * @return
     *
     * 管理员的删除功能
     */
    @DeleteMapping("/removeManager/{id}")
    public ResponseResult<Void> removeManager(@PathVariable(value = "id") Long id){

        ResponseResult<Void> result = new ResponseResult<>() ;

        try {
            this.managerService.removeManager(id) ;
            result.setCode(200);
            result.setMsg("恭喜你,删除成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("抱歉,删除失败");
        }

        return result ;
    }
}
